<?php

namespace Admin\Controller;

use Think\Controller;

class IndustryController extends Controller {

    function __construct() {
        //继承父类
        parent::__construct();

        //判断登录状态
        if (!D('Admin')->islogin()) {//未登录
            $this->error('您尚未登录，请先登录！', U('login/login'), 3);
        }
        //判断操作是否授权
        if (!D('Admin')->isoperate(CONTROLLER_NAME, ACTION_NAME)) {//未授权
            $this->error('您尚无此操作权限！', U('index/welcome'), 3);
        }
    }

    //空方法，防止报错
    public function _empty() {
        $this->index();
    }

    //行业信息
    public function index() {
        $where="1 and indus_pid=0 ";
        $info=I("info");
        if ($info){
            @extract($info);
            if($keyword=trim($keyword)){$where.=" and indus_title like '%$keyword%' ";$this->assign('keyword',$keyword);}
        }

        $industry=M('Industry');
        // 查询满足要求的总记录数
        $count      = $industry->where($where)->count();
        $Page       = new \Think\Page($count,15);
        //分页跳转的时候保证查询条件
        if($info){
            foreach($info as $key=>$val) {
                $Page->parameter['info['.$key.']']   =   urlencode($val);
            }
        }
        $pageshow   = $Page->adminshow();
        // 进行分页数据查询
        $listinfo = $industry->where($where)->order('indus_id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('listinfo',$listinfo);
        $this->assign('pageshow',$pageshow);

        $this->display(); 
    }

    //添加一级行业
    public function addyiji(){
        if (IS_POST){
            $info=I("info");
            $flag=M("Industry")->add($info);
            if($flag){
                $this->success('添加行业成功！',U('industry/index'),3);
            }else{
                $this->error('添加行业失败！',U('industry/index'),3);
            }
        }else{
            $redu=C('redu');
            $this->assign('redu',$redu);
            $this->display();
        }
    }

    //修改一级行业
    public function edityiji(){
        $indus_id=I('get.indus_id');
        if (IS_POST){
            $info=I("info");
            $flag=M("Industry")->where(array('indus_id'=>$indus_id))->save($info);
            if($flag){
                $this->success('修改行业成功！',U('industry/index'),3);
            }else{
                $this->error('修改行业失败！',U('industry/index'),3);
            }
        }else{
            $redu=C('redu');
            $indusinfo=M('Industry')->where(array('indus_id'=>$indus_id))->find();
            $this->assign('indusinfo',$indusinfo);
            $this->assign('redu',$redu);
            $this->display();
        }
    }

    //删除行业
    public function delindus(){
        $indus_id=I("get.indus_id");
        M('Industry')->where('indus_id='.$indus_id)->delete();
        $this->success('删除行业成功！',U('industry/index'),3);
    }

    //改变状态
    public function changestatus(){
        $indus_id=I("get.indus_id");
        $indus_isopen=I("get.indus_isopen");
        M('Industry')->where('indus_id='.$indus_id)->save(array('indus_isopen'=>$indus_isopen));
        if($indus_isopen==1){
            $this->success('转换开启成功！',U('industry/index'),3);
        }else{
            $this->success('转换测试成功！',U('industry/index'),3);    
        }
        
    }

    //二级行业列表
    public function indexerji(){
        $indus_pid=I('get.indus_pid');
        $where="1 and indus_pid=".$indus_pid;
        $info=I("info");
        if ($info){
            @extract($info);
            if($keyword=trim($keyword)){$where.=" and indus_title like '%$keyword%' ";$this->assign('keyword',$keyword);}
        }

        $industry=M('Industry');
        // 查询满足要求的总记录数
        $count      = $industry->where($where)->count();
        $Page       = new \Think\Page($count,15);
        //分页跳转的时候保证查询条件
        if($info){
            foreach($info as $key=>$val) {
                $Page->parameter['info['.$key.']']   =   urlencode($val);
            }
        }
        $pageshow   = $Page->adminshow();
        // 进行分页数据查询
        $listinfo = $industry->where($where)->order('indus_id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('listinfo',$listinfo);
        $this->assign('pageshow',$pageshow);
        $this->assign('indus_pid',$indus_pid);
        $this->display(); 
    }
    //添加二级行业
    public function adderji(){
       $indus_pid=I('get.indus_pid');
       if (IS_POST){
            $info=I("info");
            $info['indus_pid']=$indus_pid;
            $flag=M("Industry")->add($info);
            if($flag){
                $this->success('添加行业成功！',U('industry/indexerji',array('indus_pid'=>$indus_pid)),3);
            }else{
                $this->error('添加行业失败！',U('industry/indexerji',array('indus_pid'=>$indus_pid)),3);
            }
        }else{
            $redu=C('redu');
            $this->assign('redu',$redu);
            $this->assign('indus_pid',$indus_pid);
            $this->display();
        }
    }
    //修改二级行业
    public function editerji(){
        $indus_id=I('get.indus_id');
        $indus_pid=I('get.indus_pid');
        if (IS_POST){
            
            $info=I("info");
            $flag=M("Industry")->where(array('indus_id'=>$indus_id))->save($info);
            if($flag){
                $this->success('修改行业成功！',U('industry/indexerji',array('indus_pid'=>$indus_pid)),3);
            }else{
                $this->error('修改行业失败！',U('industry/indexerji',array('indus_pid'=>$indus_pid)),3);
            }
        }else{
            $redu=C('redu');
            $indusinfo=M('Industry')->where(array('indus_id'=>$indus_id))->find();
            $this->assign('indusinfo',$indusinfo);
            $this->assign('redu',$redu);
            $this->display();
        }
    }

    //删除二级行业
    public function delerji(){
       $indus_id=I("get.indus_id");
       $indus_pid=I("get.indus_pid");
        M('Industry')->where('indus_id='.$indus_id)->delete();
        $this->success('删除行业成功！',U('industry/indexerji',array('indus_pid'=>$indus_pid)),3);
    }


    //改变状态
    public function changeerjistatus(){
        $indus_id=I("get.indus_id");
        $indus_pid=I('get.indus_pid');
        $indus_isopen=I("get.indus_isopen");
        M('Industry')->where('indus_id='.$indus_id)->save(array('indus_isopen'=>$indus_isopen));
        if($indus_isopen==1){
            $this->success('转换开启成功！',U('industry/indexerji',array('indus_pid'=>$indus_pid)),3);
        }else{
            $this->success('转换测试成功！',U('industry/indexerji',array('indus_pid'=>$indus_pid)),3);    
        }
        
    }

    //选择管理员
    public function set_evaluate(){
        $where ="1 ";
        $admin = M('admin');
        // 查询满足要求的总记录数
        $count = $admin->where($where)->count();
        $Page = new \Think\Page($count, 20);
        $pageshow = $Page->adminshow();
        // 进行分页数据查询
        $listinfo = $admin->where($where)->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('listinfo', $listinfo);
        $this->assign('pageshow', $pageshow);
        $this->display();
    }
 
    //行业项目统计
    public function countcompany(){
        $where="1 and indus_pid=0 ";
        $info=I("info");
        if ($info){
            @extract($info);
            if($keyword=trim($keyword)){$where.=" and indus_title like '%$keyword%' ";$this->assign('keyword',$keyword);}
        }

        $industry=M('Industry');
        // 查询满足要求的总记录数
        $count      = $industry->where($where)->count();
        $Page       = new \Think\Page($count,15);
        //分页跳转的时候保证查询条件
        if($info){
            foreach($info as $key=>$val) {
                $Page->parameter['info['.$key.']']   =   urlencode($val);
            }
        }
        $pageshow   = $Page->adminshow();
        // 进行分页数据查询
        $listinfo = $industry
                  ->where($where)
                  ->order('indus_id desc')
                  ->limit($Page->firstRow.','.$Page->listRows)
                  ->select();

        foreach($listinfo as $k=>$v){
                $geshu=M('Iteminfo')->where(array('indus_id'=>$v['indus_id']))->count('item_id');
                $listinfo[$k]['geshu']=$geshu;

                $child=M("Industry")->where("indus_pid=".$v['indus_id'])->select();
                $listinfo[$k]['childnum']=sizeof($child);
            }

        $this->assign('listinfo',$listinfo);
        $this->assign('pageshow',$pageshow);

        $this->display(); 

    }

    //子行业项目统计
    public function childcountcompany(){
        $indus_pid=I('get.indus_pid');
        $where="1 and indus_pid=".$indus_pid;
        $info=I("info");
        if ($info){
            @extract($info);
            if($keyword=trim($keyword)){$where.=" and indus_title like '%$keyword%' ";$this->assign('keyword',$keyword);}
        }

        $industry=M('Industry');
        // 查询满足要求的总记录数
        $count      = $industry->where($where)->count();
        $Page       = new \Think\Page($count,15);
        //分页跳转的时候保证查询条件
        if($info){
            foreach($info as $key=>$val) {
                $Page->parameter['info['.$key.']']   =   urlencode($val);
            }
        }
        $pageshow   = $Page->adminshow();
        // 进行分页数据查询
        $listinfo = $industry
                  ->where($where)
                  ->order('indus_id desc')
                  ->limit($Page->firstRow.','.$Page->listRows)
                  ->select();

        foreach($listinfo as $k=>$v){
                $geshu=M('Iteminfo')->where(array('indus_id'=>$v['indus_id']))->count('item_id');
                $listinfo[$k]['geshu']=$geshu;
            }
        $this->assign('listinfo',$listinfo);
        $this->assign('pageshow',$pageshow);

        $this->display(); 

    }

    //月项目统计
    public function itemaddtime(){
        $where="1 ";
        $info=I("info");
        if ($info){
            @extract($info);
            // if($keyword){$where.="and username like '%$keyword%' ";$this->assign('keyword',$keyword);}
             if($indus_id){$where.="and indus_id=$indus_id ";$this->assign('indus_id',$indus_id);}
            if($starttime){$this->assign('starttime',$starttime);}
            if($endtime){$this->assign('endtime',$endtime);}
        }
        if(!$starttime){
            $starttime=date("Y-m",strtotime("-10 month"));$this->assign('starttime',$starttime);
        }
        if(!$endtime){
            $endtime=date("Y-m",SYS_TIME);$this->assign('endtime',$endtime);
        }
        $this->assign('title',"项目提交".$starttime."至".$endtime);

        $daylist=$this->get_daylist($starttime,$endtime);

        $infolist=array();
        foreach($daylist as $k=>$v){
           
            $where_=$where;
            $where_.="and item_addtime>=".strtotime($v)." ";
            $where_.="and item_addtime<=".strtotime($v."30 days")." ";
            $infolist[$k] = M("Iteminfo")->where($where_)->count();
        }
        $indusinfo=M('Iteminfo as b')
                  ->join('left join ec_industry as a on a.indus_id=b.indus_id')
                  ->field('a.indus_id,a.indus_title')
                  ->group('b.indus_id')
                  ->select();
        $this->assign('indusinfo',$indusinfo);
        $this->assign('daylist',json_encode($daylist));
        $this->assign('infolist',json_encode($infolist));
        
        $this->display();
    }

    ///////////////////////////////////////////////////////////////////////
    //获取两个日期段内所有日期
    public function get_daylist($startdate,$enddate){
        $stimestamp = strtotime($startdate);
        $etimestamp = strtotime($enddate);
        // 计算日期段内有多少天
        $days = ($etimestamp-$stimestamp)/2592000;
        // 保存每天日期
        $date = array();
        for($i=0; $i<$days; $i++){
            $date[] = date('Y-m', $stimestamp+(2592000*($i+1)));
        }
        return $date;
    }

}
